<include file="../Public/_top" />
<div class="content">
  <div class="wrap">
    <include file="./_left" />
    <div class="con_main">
      <div class="con_tit">
        <ul class="con_link">
          <li class="on"><a href="javascript:;">班级列表</a></li>
          <li><a href="{:U('public/Index/addClass')}">新建班级</a></li>
          <li><a href="{:U('public/Index/recoveryClass')}">恢复班级数据</a></li>
        </ul>
      </div>
      <div style="width:100%;height:70px;padding:15px;">
        <div class="sel_box">
          年级类型：
          <select id="class_type" class="sel" onchange="selectClass();">
            <option value="">--------请选择--------</option>
            <option value="1">大班</option>
            <option value="2">中班</option>
            <option value="3">小班</option>
            <option value="4">混龄班</option>
          </select>
        </div>
      </div>
      <div class="con_tit">
        <table class="table table_title t61">
          <tr>
            <td>
              <input id="select-all" type="checkbox" value="1" />
            </td>
            <td>班级id</td>
            <td>建档年份</td>
            <td>班级名称</td>
            <td>班级状态</td>
            <td>登录名</td>
            <td>登录密码</td>
            <td>操作</td>
          </tr>
        </table>
      </div>
      <div class="con_box" id="grade_list"></div>
      <div class="con_box" style="margin-top:-60px;">
        <div class="action_link">
          <a id="class-upgrade" class="disabled" style="width:100px;" href="javascript:upgrade();">升年级</a>
        </div>
      </div>
    </div>
  </div>
</div>
<div class="pop_wrap" id="pop_wrap" style="display:none;">
  <div class="pop_box">
    <div class="pop_head"><a class="close" href="javascript:close();">X</a><span id="titile"></span></div>
    <div class="pop_body">
      <dl style="margin-bottom:20px;text-align:center;">
        <dt>建档年份：
          <select id="classtype" class="sel">
            <option value="">-----请选择-----</option>
            <option value="1">大班</option>
            <option value="2">中班</option>
            <option value="3">小班</option>
            <option value="4">混龄班</option>
          </select>
        </dt>
      </dl>
      <dl style="margin-bottom:15px;text-align:center;">
        <dt>班级名称：<input type="text" style="height:30px;" id="class_name"></dt>
      </dl>
      <dl style="margin-bottom:15px;text-align:center;">
        <dt>登录名：&nbsp;&nbsp;<input type="text" style="height:30px;" id="login" placeholder="英文字母、数字、下划线组成"></dt>
      </dl>
      <dl style="margin-bottom:15px;text-align:center;">
        <dt>登录密码：<input type="text" style="height:30px;" id="pwd"></dt>
      </dl>
    </div>
    <ul class="pop_foot">
      <input type="hidden" id="id">
      <li><a href="javascript:close();">取消</a></li>
      <li><a href="javascript:doUpdateClass();">确定</a></li>
    </ul>
  </div>
</div>
<script>
  $('#select-all').click(function () {
    var checked = $(this).attr('checked')
    if (checked) {
      $(this).attr('checked', 'checked')
      $('.select-item').attr('checked', 'checked')
    } else {
      $(this).attr('checked', false)
      $('.select-item').attr('checked', false)
    }

    var ids = []
    $('.select-item').each(function () {
      if ($(this).attr('checked') == 'checked') {
        ids.push($(this).val())
      }
    })

    if (ids.length > 0) {
      $('#class-upgrade').removeClass('disabled').addClass('upgrade')
    } else {
      $('#class-upgrade').removeClass('upgrade').addClass('disabled')
    }
  })

  $(document).on('click', '.select-item', function () {
    var ids = []
    $('.select-item').each(function () {
      if ($(this).attr('checked') == 'checked') {
        ids.push($(this).val())
      }
    })

    if (ids.length > 0) {
      $('#class-upgrade').removeClass('disabled').addClass('upgrade')
    } else {
      $('#class-upgrade').removeClass('upgrade').addClass('disabled')
    }
  })

  function selectClass() {
    var class_type = $("#class_type").val();
    if (class_type) {
      $.ajax({
        type: "GET",
        url: "{:U('public/Index/getClassList')}",
        data: "p=1&class_type=" + class_type,
        dataType: "json",
        success: function (data) {
          $('#select-all').attr('checked', false)
          appendHtml(data);
        }
      });
    }
  }
  //追加html
  function appendHtml(data) {
    $("#grade_list").html("");
    $("#grade_list").append(data.data);
  }
  function close() {
    $("#id").val("");
    $("#class_name").val("");
    $("#pop_wrap").css("display", "none");
  }
  function addClass() {
    $("#titile").html("新建班级");
    $("#pop_wrap").css("display", "block");
  }
  function doUpdateClass() {
    var classtype = $("#classtype").val();
    var class_name = $("#class_name").val();
    var login = $("#login").val();
    var pwd = $("#pwd").val();
    if (class_type == 0) {
      alert("请选择年级");
    }
    if (class_name == "") {
      alert("请输入班级名称");
    }
    if (login == "") {
      alert("请输入登录账号");
    }
    if (pwd == "") {
      alert("请输入登录密码");
    }
  }
  function DeleteClass(id, admin_id, type) {
    var str = "";
    if (type == 1) {
      str = "你确定要删除这个班级？";
    } else {
      str = "你确定个班级已毕业？";
    }
    if (confirm(str)) {
      $.ajax({
        type: "POST",
        url: "{:U('public/Index/DeleteClass')}",
        data: "id=" + id + "&admin_id=" + admin_id + "&type=" + type,
        dataType: "text",
        success: function (data) {
          if (isNaN(data)) {
            alert(data);
          } else {
            window.location.href = window.location.href;
          }
        }
      });
    }
  }

  function GraduationClass(id) {
    if (confirm('你确定这个班级已毕业？')) {
      $.ajax({
        type: "POST",
        url: "{:U('public/Index/Graduation')}",
        data: "id=" + id,
        dataType: "text",
        success: function (data) {
          if (isNaN(data)) {
            alert(data);
          } else {
            window.location.href = window.location.href;
          }
        }
      });
    }
  }

  function RevocationGraduation(id) {
    if (confirm('你确定撤销这个班级毕业？')) {
      $.ajax({
        type: "POST",
        url: "{:U('public/Index/revocation_graduation')}",
        data: "id=" + id,
        dataType: "text",
        success: function (data) {
          if (isNaN(data)) {
            alert(data);
          } else {
            window.location.href = window.location.href;
          }
        }
      });
    }
  }

  function upgrade(id) {
    var class_type = $("#class_type").val();
    var ids = []
    $('.select-item').each(function () {
      if ($(this).attr('checked') == 'checked') {
        ids.push($(this).val())
      }
    })
    console.log('ids')
    if (ids.length > 0) {
      if (confirm('温馨提示：所选班级将统一升年级。如确认请点击确定')) {
        $.ajax({
          type: "POST",
          url: "{:U('public/Index/upgrade')}",
          data: "ids=" + ids.join(',') + '&class_type=' + class_type,
          dataType: "text",
          success: function (data) {
            if (isNaN(data)) {
              alert(data);
            } else {
              window.location.href = window.location.href;
            }
          }
        });
      }
    } else {
      alert('请选择班级');
    }
  }
</script>
<include file="../Public/_foot" />